<template>
    <div class="progress-bar" ref="progressBar" @click.stop="progressClick">
        <div class="bar-inner">
            <div class="progress" ref="progress"></div>
            <div class="progress-btn-wrapper" ref="progressBtn"
                @touchstart.prevent="progressTouchStart"
                @touchmove.prevent="progressTouchMove"
                @touchend.prevent="progressTouchEnd"
            >
                <div class="progress-btn"></div>
            </div>
        </div>
    </div>
</template>

<script>
const progressBtnWidth = 16;
export default {
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  created() {
    this.touch = {};

   
  },
  mounted() {
    let offsetWidth = this.percent / 100 * (this.$refs.progressBar.clientWidth-progressBtnWidth);
    console.log(offsetWidth);
    this.$refs.progress.style.width = `${offsetWidth}px`;
    let offsetBtn = `translate3d(${offsetWidth}px,0,0)`;
    this.$refs.progressBtn.style.transform = offsetBtn;
  },
  methods: {
    progressTouchStart(e) {
      this.touch.initated = true;
      this.touch.startX = e.touches[0].pageX;
      this.touch.left = this.$refs.progress.clientWidth;
      console.log(e.touches[0],this.touch.left)
    },
    progressTouchMove(e) {
      if (!this.touch.initated) {
        return;
      }
      const deltaX = e.touches[0].pageX - this.touch.startX;
      const offsetWidth = Math.min(
        this.$refs.progressBar.clientWidth - progressBtnWidth,
        Math.max(0, this.touch.left + deltaX)
      );
      this._offset(offsetWidth);
      this._triggerPrecent();
    },
    progressTouchEnd(e) {
      this.touch.initiated = false;
      console.log("fsd");
      
    },
    progressClick(e) {
      const rect = this.$refs.progressBar.getBoundingClientRect();
      const offsetWidth = e.pageX - rect.left;
      this._offset(offsetWidth);

      this._triggerPrecent();
    },
    _offset(offsetWidth) {
      this.$refs.progress.style.width = `${offsetWidth}px`;
      let offsetBtn = `translate3d(${offsetWidth}px,0,0)`;
      this.$refs.progressBtn.style.transform = offsetBtn;
    },
    _triggerPrecent() {
      const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth;
      const percent = this.$refs.progress.clientWidth / barWidth;
  
      this.$emit("precentChange", percent);
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';

.progress-bar {
    height: 30px;

    .bar-inner {
        position: relative;
        top: 13px;
        height: 4px;
        background: rgba(0, 0, 0, 0.3);

        .progress {
            position: absolute;
            height: 100%;
            background: $color-theme;
        }

        .progress-btn-wrapper {
            position: absolute;
            left: -8px;
            top: -13px;
            width: 30px;
            height: 30px;

            .progress-btn {
                position: relative;
                top: 7px;
                left: 7px;
                box-sizing: border-box;
                width: 16px;
                height: 16px;
                border: 3px solid $color-text;
                border-radius: 50%;
                background: $color-theme;
            }
        }
    }
}
</style>
